package com.huijun.product.service.impl;

import com.huijun.product.service.CategoryBrandRelationService;
import com.huijun.product.vo.Catelog2Vo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huijun.common.utils.PageUtils;
import com.huijun.common.utils.Query;

import com.huijun.product.dao.CategoryDao;
import com.huijun.product.entity.CategoryEntity;
import com.huijun.product.service.CategoryService;


@Service("categoryService")
public class CategoryServiceImpl extends ServiceImpl<CategoryDao, CategoryEntity> implements CategoryService {

    @Autowired
    private CategoryBrandRelationService categoryBrandRelationService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryEntity> page = this.page(
                new Query<CategoryEntity>().getPage(params),
                new QueryWrapper<CategoryEntity>()
        );

        return new PageUtils(page);
    }

    public List<CategoryEntity> treeList() {
        List<CategoryEntity> entities = baseMapper.selectList(null);
        List<CategoryEntity> level1 = entities.stream().filter(categoryEntity ->
                categoryEntity.getParentCid() == 0
        ).map((menu) -> {
            menu.setChilden(getChilden(menu, entities));
            return menu;
        }).sorted((m1, m2) -> {
            return (m1.getSort() == null ? 0 : m1.getSort()) - (m2.getSort() == null ? 0 : m2.getSort());
        }).collect(Collectors.toList());
        return level1;
    }

    private List<CategoryEntity> getChilden(CategoryEntity root, List<CategoryEntity> all) {
        List<CategoryEntity> list = all.stream().filter(categoryEntity -> {
            return categoryEntity.getParentCid() == root.getCatId();
        }).map((categoryEntity) -> {
            categoryEntity.setChilden(getChilden(categoryEntity, all));
            return categoryEntity;
        }).sorted((m1, m2) -> {
            return (m1.getSort() == null ? 0 : m1.getSort()) - (m2.getSort() == null ? 0 : m2.getSort());
        }).collect(Collectors.toList());
        return list;
    }

    public List<CategoryEntity> test1() {
        List<CategoryEntity> categoryEntities = baseMapper.selectList(new QueryWrapper<CategoryEntity>().eq("parent_cid", 0l));
       /* for (CategoryEntity categoryEntity : categoryEntities) {
            getChind3(categoryEntity);
        }
        return categoryEntities;*/

       List<CategoryEntity> list = baseMapper.selectList(null);
        ArrayList<CategoryEntity> result = new ArrayList<>();
        for (CategoryEntity categoryEntity : list) {
            if (categoryEntity.getParentCid()==0l) {
                categoryEntity.setChilden(getChilden1(categoryEntity,list));
                result.add(categoryEntity);
            }
        }
        return result;
    }

    @Override
    public List<CategoryEntity> test() {
        return baseMapper.selectList(new QueryWrapper<CategoryEntity>().eq("parent_cid",1433l));
    }


    private List<CategoryEntity> getChilden1(CategoryEntity root, List<CategoryEntity> list) {
        ArrayList<CategoryEntity> list1 = new ArrayList<>();
        for (CategoryEntity categoryEntity : list) {
            if (categoryEntity.getParentCid().equals(root.getCatId())) {
                categoryEntity.setChilden(getChilden1(categoryEntity,list));
                list1.add(categoryEntity);
            }
        }
        return list1;
    }


    private void getChind3(CategoryEntity categoryEntity) {

        List<CategoryEntity> entities = baseMapper.selectList(new QueryWrapper<CategoryEntity>().eq("parent_cid", categoryEntity.getCatId()));
       /* if (categoryEntity.getCatId().equals(1433l)) {
            System.out.println(entities);
        }*/
        if (entities.size() > 0) {
            categoryEntity.setChilden(entities);
            for (CategoryEntity entity : entities) {
                getChind3(entity);
            }
        }
    }

    @Override
    public void del(Long cid) {
        List list = new ArrayList();
        list.add(cid);
        getIds(cid,list);
        System.out.println(list);
        baseMapper.deleteBatchIds(list);
    }

    @Override
    public List<CategoryEntity> getTree() {
        QueryWrapper<CategoryEntity> wrapper = new QueryWrapper<CategoryEntity>().eq("parent_cid", 0);
        List<CategoryEntity> list = baseMapper.selectList(wrapper);
        for (CategoryEntity categoryEntity : list) {
            getChind4(categoryEntity);
        }
        return list;
    }

    @Override
    public Long[] findCatelogPath(Long categoryId) {
        List<Long> paths = new ArrayList<>();
        List<Long> parentPath = findParentPath(categoryId, paths);
        Collections.reverse(parentPath);
        return parentPath.toArray(new Long[parentPath.size()]);
    }

    @Override
    public void updateCascade(CategoryEntity category) {
        this.updateById(category);
        categoryBrandRelationService.updateCategory(category.getCatId(),category.getName());
    }

    @Override
    public List<CategoryEntity> getLevel1Category() {
        QueryWrapper<CategoryEntity> wrapper = new QueryWrapper<CategoryEntity>().eq("parent_cid", 0L);
        List<CategoryEntity> categoryEntities = baseMapper.selectList(wrapper);
        return categoryEntities;
    }

    @Override
    public Map<String, List<Catelog2Vo>> getCatalogJson() {
        List<CategoryEntity> list = this.getLevel1Category();
        Map<String, List<Catelog2Vo>> parent_cid = list.stream().collect(Collectors.toMap(k -> k.getCatId().toString(), v -> {
            QueryWrapper<CategoryEntity> wrapper = new QueryWrapper<CategoryEntity>().eq("parent_cid", v.getCatId());
            List<CategoryEntity> categoryEntityList = baseMapper.selectList(wrapper);
            List<Catelog2Vo> collect = null;
            if (categoryEntityList.size() > 0) {
                collect = categoryEntityList.stream().map(item -> {
                    Catelog2Vo catelog2Vo = new Catelog2Vo();
                    catelog2Vo.setCatalog1Id(v.getCatId().toString());
                    catelog2Vo.setId(item.getCatId().toString());
                    catelog2Vo.setName(item.getName());
                    QueryWrapper<CategoryEntity> wrapper1 = new QueryWrapper<CategoryEntity>().eq("parent_cid", item.getCatId());
                    List<CategoryEntity> categoryEntities = baseMapper.selectList(wrapper1);
                    List<Catelog2Vo.Category3Vo> collect1 = categoryEntities.stream().map(l3 -> {
                        Catelog2Vo.Category3Vo category3Vo = new Catelog2Vo.Category3Vo();
                        category3Vo.setName(l3.getName());
                        category3Vo.setId(l3.getCatId().toString());
                        category3Vo.setCatalog2Id(item.getCatId().toString());
                        return category3Vo;
                    }).collect(Collectors.toList());
                    catelog2Vo.setCatalog3List(collect1);
                    return catelog2Vo;
                }).collect(Collectors.toList());
            }
            return collect;
        }));
        return parent_cid;
    }


    public Map<String, List<Catelog2Vo>> getCatalogJson1() {
        List<CategoryEntity> list = this.getLevel1Category();
        Map<String, List<Catelog2Vo>> map = new HashMap<String, List<Catelog2Vo>>();
        for (CategoryEntity categoryEntity : list) {
            QueryWrapper<CategoryEntity> wrapper = new QueryWrapper<CategoryEntity>().eq("parent_cid", categoryEntity.getCatId());
            List<CategoryEntity> level2List = baseMapper.selectList(wrapper);
            List<Catelog2Vo> catelog2Vos = new ArrayList<>();
            for (CategoryEntity entity : level2List) {
                Catelog2Vo catelog2Vo = new Catelog2Vo();
                catelog2Vo.setId(entity.getCatId().toString());
                catelog2Vo.setName(entity.getName());
                catelog2Vo.setCatalog1Id(categoryEntity.getCatId().toString());
                QueryWrapper<CategoryEntity> wrapper1 = new QueryWrapper<CategoryEntity>().eq("parent_cid", entity.getCatId());
                List<CategoryEntity> level3List = baseMapper.selectList(wrapper1);
                ArrayList<Catelog2Vo.Category3Vo> category3Vos = new ArrayList<>();
                for (CategoryEntity categoryEntity1 : level3List) {
                    Catelog2Vo.Category3Vo category3Vo = new Catelog2Vo.Category3Vo();
                    category3Vo.setId(categoryEntity1.getCatId().toString());
                    category3Vo.setName(categoryEntity1.getName());
                    category3Vo.setCatalog2Id(entity.getCatId().toString());
                    category3Vos.add(category3Vo);
                }
                catelog2Vo.setCatalog3List(category3Vos);
                catelog2Vos.add(catelog2Vo);
            }
            map.put(categoryEntity.getCatId().toString(),catelog2Vos);
        }
        return map;
    }




    private List<Long> findParentPath(Long categoryId, List<Long> paths) {
        //1、收集当前节点id
        paths.add(categoryId);
        CategoryEntity byId = this.getById(categoryId);
        if(byId.getParentCid()!=0){
            findParentPath(byId.getParentCid(),paths);
        }
        return paths;
    }

    private void getChind4(CategoryEntity categoryEntity) {
        QueryWrapper<CategoryEntity> wrapper = new QueryWrapper<CategoryEntity>().eq("parent_cid", categoryEntity.getCatId());
        List<CategoryEntity> list = baseMapper.selectList(wrapper);
        if (list.size()>0) {
            categoryEntity.setChilden(list);
            for (CategoryEntity entity : list) {
                getChind4(entity);
            }
        }



/*
        List<CategoryEntity> entities = baseMapper.selectList(new QueryWrapper<CategoryEntity>().eq("parent_cid", categoryEntity.getCatId()));
       *//* if (categoryEntity.getCatId().equals(1433l)) {
            System.out.println(entities);
        }*//*
        if (entities.size() > 0) {
            categoryEntity.setChilden(entities);
            for (CategoryEntity entity : entities) {
                getChind3(entity);
            }
        }*/
    }


    private void getIds(Long cid, List list) {
        QueryWrapper<CategoryEntity> wrapper = new QueryWrapper<CategoryEntity>().eq("parent_cid", cid);
        List<CategoryEntity> selectList = baseMapper.selectList(wrapper);
        if (selectList.size()>0) {
            for (CategoryEntity categoryEntity : selectList) {
                list.add(categoryEntity.getCatId());
                this.getIds(categoryEntity.getCatId(),list);
            }
        }
    }


}